package com.erp.pp.service.impl;

import com.erp.pp.entity.TPpOrderBomEntity;
import com.erp.pp.entity.TPpOrderEntity;
import com.erp.pp.entity.TPpOrderRoutingEntity;
import com.erp.pp.service.TPpOrderServiceI;
import com.erp.util.erpUtil;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;


@Service("tPpOrderService")
@Transactional
public class TPpOrderServiceImpl extends CommonServiceImpl implements TPpOrderServiceI {
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((TPpOrderEntity)entity);
 	}
	
	public void addMain(TPpOrderEntity tPpOrder,
	        List<TPpOrderBomEntity> tPpOrderBomList,List<TPpOrderRoutingEntity> tPpOrderRoutingList){
			//保存主信息
			this.save(tPpOrder);
		
			/**保存-BOM项目*/
			for(TPpOrderBomEntity tPpOrderBom:tPpOrderBomList){
				//外键设置
				tPpOrderBom.setPrdorderid(tPpOrder.getId());
				this.save(tPpOrderBom);
			}
			/**保存-加工工序*/
			for(TPpOrderRoutingEntity tPpOrderRouting:tPpOrderRoutingList){
				//外键设置
				tPpOrderRouting.setPrdorderid(tPpOrder.getId());
				this.save(tPpOrderRouting);
			}
			//执行新增操作配置的sql增强
 			this.doAddSql(tPpOrder);
	}

	
	public void updateMain(TPpOrderEntity tPpOrder,
	        List<TPpOrderBomEntity> tPpOrderBomList,List<TPpOrderRoutingEntity> tPpOrderRoutingList) {
		//保存主表信息
		if(StringUtil.isNotEmpty(tPpOrder.getId())){
			try {
				TPpOrderEntity temp = findUniqueByProperty(TPpOrderEntity.class, "id", tPpOrder.getId());
				MyBeanUtils.copyBeanNotNull2Bean(tPpOrder, temp);
				this.saveOrUpdate(temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			this.saveOrUpdate(tPpOrder);
		}
		//===================================================================================
		//获取参数
		Object id0 = tPpOrder.getId();
		Object id1 = tPpOrder.getId();
		//===================================================================================
		//1.查询出数据库的明细数据-BOM项目
	    String hql0 = "from TPpOrderBomEntity where 1 = 1 AND pRDORDERID = ? ";
	    List<TPpOrderBomEntity> tPpOrderBomOldList = this.findHql(hql0,id0);
		//2.筛选更新明细数据-BOM项目
		if(tPpOrderBomList!=null&&tPpOrderBomList.size()>0){
		for(TPpOrderBomEntity oldE:tPpOrderBomOldList){
			boolean isUpdate = false;
				for(TPpOrderBomEntity sendE:tPpOrderBomList){
					//需要更新的明细数据-BOM项目
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-BOM项目
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-BOM项目
			for(TPpOrderBomEntity tPpOrderBom:tPpOrderBomList){
				if(oConvertUtils.isEmpty(tPpOrderBom.getId())){
					//外键设置
					tPpOrderBom.setPrdorderid(tPpOrder.getId());
					this.save(tPpOrderBom);
				}
			}
		}
		//===================================================================================
		//1.查询出数据库的明细数据-加工工序
	    String hql1 = "from TPpOrderRoutingEntity where 1 = 1 AND pRDORDERID = ? ";
	    List<TPpOrderRoutingEntity> tPpOrderRoutingOldList = this.findHql(hql1,id1);
		//2.筛选更新明细数据-加工工序
		if(tPpOrderRoutingList!=null&&tPpOrderRoutingList.size()>0){
		for(TPpOrderRoutingEntity oldE:tPpOrderRoutingOldList){
			boolean isUpdate = false;
				for(TPpOrderRoutingEntity sendE:tPpOrderRoutingList){
					//需要更新的明细数据-加工工序
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-加工工序
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-加工工序
			for(TPpOrderRoutingEntity tPpOrderRouting:tPpOrderRoutingList){
				if(oConvertUtils.isEmpty(tPpOrderRouting.getId())){
					//外键设置
					tPpOrderRouting.setPrdorderid(tPpOrder.getId());
					this.save(tPpOrderRouting);
				}
			}
		}
		//执行更新操作配置的sql增强
 		this.doUpdateSql(tPpOrder);
	}

	
	public void delMain(TPpOrderEntity tPpOrder) {
		//删除主表信息
		this.delete(tPpOrder);
		//===================================================================================
		//获取参数
		Object id0 = tPpOrder.getId();
		Object id1 = tPpOrder.getId();
		//===================================================================================
		//删除-BOM项目
	    String hql0 = "from TPpOrderBomEntity where 1 = 1 AND pRDORDERID = ? ";
	    List<TPpOrderBomEntity> tPpOrderBomOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(tPpOrderBomOldList);
		//===================================================================================
		//删除-加工工序
	    String hql1 = "from TPpOrderRoutingEntity where 1 = 1 AND pRDORDERID = ? ";
	    List<TPpOrderRoutingEntity> tPpOrderRoutingOldList = this.findHql(hql1,id1);
		this.deleteAllEntitie(tPpOrderRoutingOldList);
	}
	
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(TPpOrderEntity t){
		erpUtil.saveentity("202","A",t.getId());

		return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(TPpOrderEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(TPpOrderEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,TPpOrderEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode()));
 		sql  = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode()));
 		sql  = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus()));
 		sql  = sql.replace("#{docnum}",String.valueOf(t.getDocnum()));
 		sql  = sql.replace("#{docdate}",String.valueOf(t.getDocdate()));
 		sql  = sql.replace("#{startdate}",String.valueOf(t.getStartdate()));
 		sql  = sql.replace("#{enddate}",String.valueOf(t.getEnddate()));
 		sql  = sql.replace("#{matcode}",String.valueOf(t.getMatcode()));
 		sql  = sql.replace("#{matname}",String.valueOf(t.getMatname()));
 		sql  = sql.replace("#{quantity}",String.valueOf(t.getQuantity()));
 		sql  = sql.replace("#{unit}",String.valueOf(t.getUnit()));
 		sql  = sql.replace("#{text}",String.valueOf(t.getText()));
 		sql  = sql.replace("#{sodocnum}",String.valueOf(t.getSodocnum()));
 		sql  = sql.replace("#{soitemid}",String.valueOf(t.getSoitemid()));
 		sql  = sql.replace("#{cmptqty}",String.valueOf(t.getCmptqty()));
 		sql  = sql.replace("#{prccode}",String.valueOf(t.getPrccode()));
 		sql  = sql.replace("#{prcname}",String.valueOf(t.getPrcname()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}